package com.qqtech.qquan.activity.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qqtech.core.common.annotation.VisitAnnt;
import com.qqtech.core.common.enums.BizzLevel1Enum;
import com.qqtech.core.common.enums.RespEnum;
import com.qqtech.core.common.enums.YesNoEnum;
import com.qqtech.core.common.util.TimeUtil;
import com.qqtech.core.frame.model.PageKit;
import com.qqtech.core.frame.model.PageOrder;
import com.qqtech.core.frame.model.Result;
import com.qqtech.core.frame.service.BaseService;
import com.qqtech.qquan.activity.enums.ActivityBaseCostTypeDictEnum;
import com.qqtech.qquan.activity.enums.ActivityBaseStatusDictEnum;
import com.qqtech.qquan.activity.enums.ActivityBaseTypeDictEnum;
import com.qqtech.qquan.activity.enums.ActivityConst;
import com.qqtech.qquan.activity.model.ActivityBase;
import com.qqtech.qquan.activity.model.ActivityBaseQuery;
import com.qqtech.qquan.activity.model.ActivityBaseVo;
import com.qqtech.qquan.activity.model.ActivitySign;
import com.qqtech.qquan.activity.model.ActivitySignQuery;
import com.qqtech.qquan.activity.model.ActivitySignVo;
import com.qqtech.qquan.activity.service.ActivityBaseService;
import com.qqtech.qquan.activity.service.ActivitySignService;
import com.qqtech.qquan.activity.service.ActivitySysDictService;
import com.qqtech.qquan.circle.enums.CircleAuditStatusEnum;
import com.qqtech.qquan.circle.enums.CircleMemberTypeDictEnum;
import com.qqtech.qquan.circle.model.CircleBaseVo;
import com.qqtech.qquan.circle.model.CircleMemberQuery;
import com.qqtech.qquan.circle.model.CircleMemberVo;
import com.qqtech.qquan.circle.service.CircleBaseService;
import com.qqtech.qquan.circle.service.CircleMemberService;
import com.qqtech.qquan.community.model.CommunityBaseVo;
import com.qqtech.qquan.community.service.CommunityBaseService;
import com.qqtech.qquan.pub.model.PubConcern;
import com.qqtech.qquan.pub.model.PubPraise;
import com.qqtech.qquan.pub.service.PubCommentService;
import com.qqtech.qquan.pub.service.PubConcernService;
import com.qqtech.qquan.pub.service.PubPraiseService;
import com.qqtech.qquan.push.service.PushBizzService;
import com.qqtech.qquan.sys.enums.SysConst;
import com.qqtech.qquan.user.model.UserBase;
import com.qqtech.qquan.user.model.UserBaseVo;
import com.qqtech.qquan.user.service.UserBaseService;
import com.qqtech.web.context.BaseWebController;

@Controller
@RequestMapping(value = "/activity/base")
public class ActivityBaseController extends BaseWebController<ActivityBase, ActivityBaseQuery> {
	@Resource
	private ActivityBaseService activityBaseService;

	@Resource
	private ActivitySignService activitySignService;

	@Resource
	private UserBaseService userBaseService;

	@Resource
	private PubPraiseService pubPraiseService;

	@Resource
	private PubConcernService pubConcernService;

	@Resource
	private PubCommentService pubCommentService;

	@Resource
	private CircleBaseService circleBaseService;
	@Resource
	private CircleMemberService circleMemberService;

	@Resource
	private CommunityBaseService communityBaseService;

	@Resource
	private ActivitySysDictService activitySysDictService;
	@Resource
	private PushBizzService pushBizzService;

	@Override
	protected BaseService<ActivityBase> getBaseService() {
		return activityBaseService;
	}

	/**
	 * 分页
	 * 
	 * @param activityBaseQuery
	 * @return
	 */
	@RequestMapping(value = "/page")
	@ResponseBody
	public Result page(@RequestBody ActivityBaseQuery activityBaseQuery) {
		Result result = new Result();
		activityBaseQuery.setIsShow(YesNoEnum.YES.getCode());
		Pageable pageKit = new PageKit(activityBaseQuery.getPage(), new PageOrder(Direction.ASC, "recommendOidx"),
				new PageOrder(Direction.ASC, "status"), new PageOrder(Direction.DESC, "id"));
		Page<ActivityBaseVo> page = activityBaseService.queryPageList(activityBaseQuery, pageKit);
		String typeName = "";
		for (ActivityBaseVo activityBaseVo : page) {
			if (activityBaseVo.getCostType() == ActivityBaseCostTypeDictEnum.ONLINE.getCode()) {// 在线交易
				activityBaseVo.setShowCost("￥" + activityBaseVo.getCost());
			} else {
				typeName = activitySysDictService.dictValue(ActivityConst.DICTYPE_ACTIVITY_BASE_COSTTYPE,
						String.valueOf(activityBaseVo.getCostType()));
				activityBaseVo.setShowCost(typeName);
			}
			if (activityBaseVo.getType() == ActivityBaseTypeDictEnum.COMMUNITY.getCode()) {// 社区活动
				UserBaseVo userBaseVo = userBaseService.getById(activityBaseVo.getCreatorId());
				activityBaseVo.setSponsorLogo(userBaseVo.getFullLogo());
				activityBaseVo.setSponsorName(userBaseVo.getNick());
				activityBaseVo.setSponsorId(userBaseVo.getId());
			} else {// 圈子活动
				CircleBaseVo circleBaseVo = circleBaseService.getById(activityBaseVo.getBizzId());
				activityBaseVo.setSponsorLogo(circleBaseVo.getFullLogo());
				activityBaseVo.setSponsorName(circleBaseVo.getName());
				activityBaseVo.setSponsorId(circleBaseVo.getId());
			}
			activityBaseVo.setIsShowPraiseList(false);
			addCommentAndPraiseInfo(activityBaseVo);
		}
		result.put("page", page);
		return result;
	}

	/**
	 * 分页
	 * 
	 * @param activityBaseQuery
	 * @return
	 */
	@RequestMapping(value = "/pageBycircle")
	@ResponseBody
	public Result pageBycircle(@RequestBody ActivityBaseQuery activityBaseQuery) {
		Result result = new Result();
		activityBaseQuery.setIsShow(YesNoEnum.YES.getCode());
		Pageable pageKit = new PageKit(activityBaseQuery.getPage(), new PageOrder(Direction.ASC, "a.recommendOidx"),
				new PageOrder(Direction.ASC, "a.status"), new PageOrder(Direction.DESC, "a.id"));
		Page<ActivityBaseVo> page = activityBaseService.queryPageByCircle(activityBaseQuery, pageKit);
		String typeName = "";
		for (ActivityBaseVo activityBaseVo : page) {
			if (activityBaseVo.getCostType() == ActivityBaseCostTypeDictEnum.ONLINE.getCode()) {// 在线交易
				activityBaseVo.setShowCost("￥" + activityBaseVo.getCost());
			} else {
				typeName = activitySysDictService.dictValue(ActivityConst.DICTYPE_ACTIVITY_BASE_COSTTYPE,
						String.valueOf(activityBaseVo.getCostType()));
				activityBaseVo.setShowCost(typeName);
			}
			if (activityBaseVo.getType() == ActivityBaseTypeDictEnum.COMMUNITY.getCode()) {// 社区活动
				UserBaseVo userBaseVo = userBaseService.getById(activityBaseVo.getCreatorId());
				activityBaseVo.setSponsorLogo(userBaseVo.getFullLogo());
				activityBaseVo.setSponsorName(userBaseVo.getNick());
				activityBaseVo.setSponsorId(userBaseVo.getId());
			} else {// 圈子活动
				CircleBaseVo circleBaseVo = circleBaseService.getById(activityBaseVo.getBizzId());
				activityBaseVo.setSponsorLogo(circleBaseVo.getFullLogo());
				activityBaseVo.setSponsorName(circleBaseVo.getName());
				activityBaseVo.setSponsorId(circleBaseVo.getId());
			}
			activityBaseVo.setIsShowPraiseList(false);
			addCommentAndPraiseInfo(activityBaseVo);
		}
		result.put("page", page);
		return result;
	}

	/**
	 * 分页
	 * 
	 * @param activityBaseQuery,我加入、我创建圈子里的活动
	 * @return
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/myCirclePage")
	@ResponseBody
	public Result myCirclePage(@RequestBody ActivityBaseQuery activityBaseQuery) {
		Result result = new Result();
		activityBaseQuery.setType(ActivityBaseTypeDictEnum.CIRCLE.getCode());
		activityBaseQuery.setIsShow(YesNoEnum.YES.getCode());
		Integer curUserId = super.getUserId();
		CircleMemberQuery query = new CircleMemberQuery();
		query.setMemberId(curUserId);
		List<Integer> myCircleIds = circleMemberService.queryMyCircleIds(query);// 我的圈子id列表
		if (myCircleIds != null && !myCircleIds.isEmpty()) {
			activityBaseQuery.setIncludeBizzIds(myCircleIds);
		} else {
			activityBaseQuery.setCreatorId(curUserId);
		}
		Pageable pageKit = new PageKit(activityBaseQuery.getPage(), new PageOrder(Direction.ASC, "status"));
		Page<ActivityBaseVo> page = activityBaseService.queryPageList(activityBaseQuery, pageKit);
		String typeName = "";
		for (ActivityBaseVo activityBaseVo : page) {
			if (activityBaseVo.getCostType() == ActivityBaseCostTypeDictEnum.ONLINE.getCode()) {// 在线交易
				activityBaseVo.setShowCost("￥" + activityBaseVo.getCost());
			} else {
				typeName = activitySysDictService.dictValue(ActivityConst.DICTYPE_ACTIVITY_BASE_COSTTYPE,
						String.valueOf(activityBaseVo.getCostType()));
				activityBaseVo.setShowCost(typeName);
			}
			if (activityBaseVo.getType() == ActivityBaseTypeDictEnum.COMMUNITY.getCode()) {// 社区活动
				UserBaseVo userBaseVo = userBaseService.getById(activityBaseVo.getCreatorId());
				activityBaseVo.setSponsorLogo(userBaseVo.getFullLogo());
				activityBaseVo.setSponsorName(userBaseVo.getNick());
				activityBaseVo.setSponsorId(userBaseVo.getId());
			} else {// 圈子活动
				CircleBaseVo circleBaseVo = circleBaseService.getById(activityBaseVo.getBizzId());
				activityBaseVo.setSponsorLogo(circleBaseVo.getFullLogo());
				activityBaseVo.setSponsorName(circleBaseVo.getName());
				activityBaseVo.setSponsorId(circleBaseVo.getId());
			}
			activityBaseVo.setIsShowPraiseList(false);
			addCommentAndPraiseInfo(activityBaseVo);
		}
		result.put("page", page);
		return result;
	}

	/**
	 * 分页 管理圈子的活动
	 * 
	 * @param activityBaseQuery
	 * @return
	 */
	@RequestMapping(value = "/page4manageCircle")
	@ResponseBody
	public Result page4manageCircle(@RequestBody ActivityBaseQuery activityBaseQuery) {
		Result result = new Result();
		// activityBaseQuery.setIsShow(YesNoEnum.YES.getCode());
		Pageable pageKit = new PageKit(activityBaseQuery.getPage(), new PageOrder(Direction.DESC, "id"));
		Page<ActivityBaseVo> page = activityBaseService.queryPageList(activityBaseQuery, pageKit);
		String typeName = "";
		for (ActivityBaseVo activityBaseVo : page) {
			if (activityBaseVo.getCostType() == ActivityBaseCostTypeDictEnum.ONLINE.getCode()) {// 在线交易
				activityBaseVo.setShowCost("￥" + activityBaseVo.getCost());
			} else {
				typeName = activitySysDictService.dictValue(ActivityConst.DICTYPE_ACTIVITY_BASE_COSTTYPE,
						String.valueOf(activityBaseVo.getCostType()));
				activityBaseVo.setShowCost(typeName);
			}
			activityBaseVo.setIsShowPraiseList(false);
			addCommentAndPraiseInfo(activityBaseVo);
		}
		result.put("page", page);
		return result;
	}

	/**
	 * 分页
	 * 
	 * @param activityBaseQuery
	 * @return
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/manage/page")
	@ResponseBody
	public Result managePage(@RequestBody ActivityBaseQuery activityBaseQuery) {
		Result result = new Result();
		Integer curUserId = super.getUserId();
		activityBaseQuery.setCreatorId(curUserId);
		activityBaseQuery.setIsShow(YesNoEnum.YES.getCode());
		Pageable pageKit = new PageKit(activityBaseQuery.getPage(), new PageOrder(Direction.DESC, "id"));
		Page<ActivityBaseVo> page = activityBaseService.queryPageList(activityBaseQuery, pageKit);
		String typeName;
		int signCount;
		for (ActivityBaseVo activityBaseVo : page) {
			if (activityBaseVo.getCostType() == ActivityBaseCostTypeDictEnum.ONLINE.getCode()) {// 在线交易
				activityBaseVo.setShowCost("￥" + activityBaseVo.getCost());
			} else {
				typeName = activitySysDictService.dictValue(ActivityConst.DICTYPE_ACTIVITY_BASE_COSTTYPE,
						String.valueOf(activityBaseVo.getCostType()));
				activityBaseVo.setShowCost(typeName);
			}
			signCount = activitySignService.getSignCount(activityBaseVo.getId());
			activityBaseVo.setSignCount(signCount);
		}
		result.put("page", page);
		return result;
	}

	/**
	 * pv+1在获取详情
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/detail")
	@ResponseBody
	public Result detail(@RequestBody ActivityBaseQuery activityBaseQuery) {
		Result result = new Result();
		Integer curUserId = super.getUserId();
		ActivityBaseVo detailVo = activityBaseService.handleDetail(activityBaseQuery.getId());
		int signCount = activitySignService.getSignCount(activityBaseQuery.getId());
		detailVo.setSignCount(signCount);
		if (null != curUserId) {
			boolean isSign = activitySignService.isSign(curUserId, activityBaseQuery.getId());
			detailVo.setIsSign(isSign);
			if (detailVo.getType() == ActivityBaseTypeDictEnum.CIRCLE.getCode()) {
				Integer circleId = detailVo.getBizzId();// 圈子ID
				boolean canDeleteActivity = false;
				if (curUserId == detailVo.getCreatorId()) {
					canDeleteActivity = true;
				} else if (circleBaseService.isCircleCreator(curUserId, circleId)) {
					canDeleteActivity = true;
				} else if (circleMemberService.isCircleManager(curUserId, circleId)) {
					canDeleteActivity = true;
				} else {
					canDeleteActivity = false;
				}
				result.put("canDeleteActivity", canDeleteActivity);
			}
		}
		Pageable pageKit = new PageKit(1, 6, new PageOrder(Direction.DESC, "id"));
		ActivitySignQuery activitySignQuery = new ActivitySignQuery();
		activitySignQuery.setActivityId(activityBaseQuery.getId());
		List<ActivitySignVo> signList = activitySignService.queryList(activitySignQuery, pageKit);
		UserBaseVo userBaseVo = null;
		for (ActivitySignVo activitySignVo : signList) {
			userBaseVo = userBaseService.getById(activitySignVo.getUserId());
			activitySignVo.setUserLogo(userBaseVo.getFullLogo());
			activitySignVo.setUserNick(userBaseVo.getNick());
		}
		if (null == signList || signList.size() <= 0) {
			signList = new ArrayList<ActivitySignVo>();
			signList.add(new ActivitySignVo());
		}
		detailVo.setSignList(signList);
		detailVo.setImgString(detailVo.getResumeImg(), SysConst.IMG_POSTFIX);
		addActivityBaseVo(detailVo);
		addCommentAndPraiseInfo(detailVo);
		addConcernInfo(detailVo);
		detailVo.setIsShowPraiseList(true);
		addPraiseUserList(detailVo);
		result.put("detailVo", detailVo);
		return result;
	}

	/**
	 * 去编辑
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/toUpdate")
	@ResponseBody
	public Result toUpdate(@RequestBody ActivityBaseQuery activityBaseQuery) {
		Result result = new Result();
		ActivityBaseVo detailVo = activityBaseService.getById(activityBaseQuery.getId());
		detailVo.setImgString(detailVo.getResumeImg(), SysConst.IMG_POSTFIX);
		if (detailVo.getType() == ActivityBaseTypeDictEnum.COMMUNITY.getCode()) {// 社区活动
			CommunityBaseVo communityBaseVo = communityBaseService.getById(detailVo.getBizzId());
			detailVo.setSponsorName(communityBaseVo.getName());
		} else {// 圈子活动
			CircleBaseVo circleBaseVo = circleBaseService.getById(detailVo.getBizzId());
			detailVo.setSponsorName(circleBaseVo.getName());
		}
		result.put("detailVo", detailVo);
		return result;
	}

	/**
	 * 去编辑
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/update")
	@ResponseBody
	public Result update(@RequestBody ActivityBase activityBase) {
		Result result = new Result();
		activityBase.setLogo(activityBase.getLogo().replaceAll(activityBase.getFileDomain(), ""));
		activityBase.setResumeImg(activityBase.getResumeImg().replaceAll(activityBase.getFileDomain(), ""));
		activityBaseService.updateById(activityBase);
		return result;
	}

	private void addActivityBaseVo(ActivityBaseVo baseVo) {
		baseVo.setShowTime(TimeUtil.time2cn4TwoDay(baseVo.getStartAt(), baseVo.getEndAt()));
		String statusName = activitySysDictService.dictValue(ActivityConst.DICTYPE_ACTIVITY_BASE_STATUS,
				String.valueOf(baseVo.getStatus()));
		baseVo.setShowStatus(statusName);
		if (baseVo.getCostType() == ActivityBaseCostTypeDictEnum.ONLINE.getCode()) {// 在线交易
			baseVo.setShowCost("￥" + baseVo.getCost());
		} else {
			String typeName = activitySysDictService.dictValue(ActivityConst.DICTYPE_ACTIVITY_BASE_COSTTYPE,
					String.valueOf(baseVo.getCostType()));
			baseVo.setShowCost(typeName);
		}
		if (baseVo.getType() == ActivityBaseTypeDictEnum.COMMUNITY.getCode()) {// 社区活动
			UserBaseVo userBaseVo = userBaseService.getById(baseVo.getCreatorId());
			baseVo.setSponsorLogo(userBaseVo.getFullLogo());
			baseVo.setSponsorName(userBaseVo.getNick());
			baseVo.setSponsorId(userBaseVo.getId());
		} else {// 圈子活动
			CircleBaseVo circleBaseVo = circleBaseService.getById(baseVo.getBizzId());
			baseVo.setSponsorLogo(circleBaseVo.getFullLogo());
			baseVo.setSponsorName(circleBaseVo.getName());
			baseVo.setSponsorId(circleBaseVo.getId());
		}
	}

	/**
	 * 删除
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/delete")
	@ResponseBody
	public Result delete(@RequestBody ActivityBaseVo activityBaseVo) {
		Result result = new Result();
		ActivityBase delVo = new ActivityBase();
		delVo.setId(activityBaseVo.getId());
		delVo.setIsShow(YesNoEnum.NO.getCode());
		delVo.setStatus(ActivityBaseStatusDictEnum.INVALID.getCode());
		activityBaseService.updateById(delVo);
		return result;
	}

	/**
	 * 新增
	 * 
	 * @param activityBaseVo
	 * @return
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/create")
	@ResponseBody
	public Result create(@RequestBody ActivityBaseVo activityBaseVo) {
		Result result = new Result();
		Integer curUserId = super.getUserId();
		activityBaseVo.setCreatorId(curUserId);
		activityBaseVo.setIsShow(YesNoEnum.YES.getCode());
		activityBaseVo.setPv(0);
		activityBaseVo.setStatus(ActivityBaseStatusDictEnum.SIGN.getCode());
		if (ActivityBaseTypeDictEnum.CIRCLE.getCode() == activityBaseVo.getType()) {// 圈子活动
			CircleBaseVo circleBaseVo = circleBaseService.getById(activityBaseVo.getBizzId());
			if (circleBaseVo.getAuditStatus() != CircleAuditStatusEnum.AUDIT_PASS.getCode()) {
				result.addErr("该圈子未审核通过。");
				return result;
			}
			CircleMemberVo circleMember = circleMemberService.getCircleMember(curUserId, circleBaseVo.getId());
			if (circleMember == null) {
				result.addErr("你不是该圈子的成员!");
				return result;
			} else if (circleMember.getType() == CircleMemberTypeDictEnum.MEMBER.getCode()) {
				result.addErr("只有圈子或管理员才能发布活动!");
				return result;
			}
		}
		UserBase userBase = userBaseService.getById(curUserId);
		if (userBase != null) {
			activityBaseVo.setCommunityId(userBase.getCommunityId());
		}
		activityBaseService.insert(activityBaseVo);
		// 发布圈子活动信息
		if (ActivityBaseTypeDictEnum.CIRCLE.getCode() == activityBaseVo.getType()) {
			CircleBaseVo circleBaseVo = circleBaseService.getById(activityBaseVo.getBizzId());
			List<CircleMemberVo> circleMemberVos = circleMemberService.queryCircleMembers(circleBaseVo.getId());
			List<Integer> userIds = new ArrayList<>();
			for (CircleMemberVo circleMemberVo : circleMemberVos) {
				userIds.add(circleMemberVo.getMemberId());
			}
			String msg = "你的圈子 " + circleBaseVo.getName() + " 有的发布新活动 " + activityBaseVo.getName();
			pushBizzService.circleActivityNotice(userIds, activityBaseVo.getId(), msg);
		}
		return result;
	}

	/**
	 * 屏蔽活动
	 * 
	 * @param activity
	 * @return by woon
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/hide")
	@ResponseBody
	public Result hide(@RequestBody ActivityBase activity) {
		Result result = new Result();
		Integer activityId = activity.getId();
		ActivityBase activityDB = activityBaseService.getById(activityId);
		if (activityDB == null) {
			result.addErr("活动不存在");
			return result;
		}
		Integer curUserId = super.getUserId();// 当前用户id
		Integer type = activityDB.getType();

		if (type == ActivityBaseTypeDictEnum.CIRCLE.getCode()) {
			Integer circleId = activityDB.getBizzId();
			// 圈主、圈子管理员、活动创建者 才有权操作
			if (circleBaseService.isCircleCreator(curUserId, circleId)
					|| circleMemberService.isCircleManager(curUserId, circleId)
					|| activityBaseService.isActivityCreator(curUserId, activityId)) {
				activityBaseService.hide(activityId);
			} else {
				result.addErr("无权操作！");
				return result;
			}
		} else if (type == ActivityBaseTypeDictEnum.COMMUNITY.getCode()) {
			Integer communityId = activityDB.getBizzId();
			// 社区创建人、社区代理材长、活动创建者 才有权操作
			if (communityBaseService.isCommunityCreator(curUserId, communityId)
					|| communityBaseService.isCommunityAgent(curUserId, communityId)
					|| activityBaseService.isActivityCreator(curUserId, activityId)) {
				activityBaseService.hide(activityId);
			} else {
				result.addErr("无权操作！");
				return result;
			}
		}
		return result;
	}

	/**
	 * 取消屏蔽活动
	 * 
	 * @param activity
	 * @return by woon
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/show")
	@ResponseBody
	public Result show(@RequestBody ActivityBase activity) {
		Result result = new Result();
		Integer activityId = activity.getId();
		ActivityBase activityDB = activityBaseService.getById(activityId);
		if (activityDB == null) {
			result.addErr("活动不存在");
			return result;
		}
		Integer curUserId = super.getUserId();// 当前用户id
		Integer type = activityDB.getType();

		if (type == ActivityBaseTypeDictEnum.CIRCLE.getCode()) {
			Integer circleId = activityDB.getBizzId();
			// 圈主、圈子管理员、活动创建者 才有权操作
			if (circleBaseService.isCircleCreator(curUserId, circleId)
					|| circleMemberService.isCircleManager(curUserId, circleId)
					|| activityBaseService.isActivityCreator(curUserId, activityId)) {
				activityBaseService.show(activityId);
			} else {
				result.addErr("无权操作！");
				return result;
			}
		} else if (type == ActivityBaseTypeDictEnum.COMMUNITY.getCode()) {
			Integer communityId = activityDB.getBizzId();
			// 社区创建人、社区代理材长、活动创建者 才有权操作
			if (communityBaseService.isCommunityCreator(curUserId, communityId)
					|| communityBaseService.isCommunityAgent(curUserId, communityId)
					|| activityBaseService.isActivityCreator(curUserId, activityId)) {
				activityBaseService.show(activityId);
			} else {
				result.addErr("无权操作！");
				return result;
			}
		}
		return result;
	}

	// 评论、点赞数据
	private void addCommentAndPraiseInfo(ActivityBaseVo vo) {
		Integer commentCount = pubCommentService.getCommentCountByBizz(vo.getId(), BizzLevel1Enum.ACTIVITY.getCode());
		vo.setReplyCount(commentCount);
		vo.setIsPraised(YesNoEnum.NO.getCode());
		List<PubPraise> praiseList = pubPraiseService.queryPraiseListByBizz(vo.getId(),
				BizzLevel1Enum.ACTIVITY.getCode());
		if (praiseList != null && praiseList.size() > 0) {
			vo.setPraiseCount(praiseList.size());
			if (!super.isVisitor()) {
				for (PubPraise pp : praiseList) {
					if (pp.getCreatorId().equals(super.getUserId())) {
						vo.setIsPraised(YesNoEnum.YES.getCode());
						break;
					}
				}
			}
		} else {
			vo.setPraiseCount(0);
		}
	}

	// 附加 关注信息
	private void addConcernInfo(ActivityBaseVo vo) {
		PubConcern pubConcern = new PubConcern();
		if (!super.isVisitor()) {
			pubConcern.setCreatorId(super.getUserId());
			pubConcern.setBizzId(vo.getId());
			pubConcern.setBizzType(BizzLevel1Enum.ACTIVITY.getCode());
			PubConcern pc = pubConcernService.getOne(pubConcern);
			if (pc == null) {
				vo.setIsConcern(YesNoEnum.NO.getCode());
			} else {
				vo.setIsConcern(YesNoEnum.YES.getCode());
			}
		} else {
			vo.setIsConcern(YesNoEnum.NO.getCode());
		}
	}

	// 附加点赞人列表
	private void addPraiseUserList(ActivityBaseVo vo) {
		List<PubPraise> praiseList = pubPraiseService.queryPraiseListByBizz(vo.getId(),
				BizzLevel1Enum.ACTIVITY.getCode());
		String praiseUserList = "";
		PubPraise praise = null;
		UserBase user = null;
		for (int i = 0; i < praiseList.size(); i++) {
			praise = praiseList.get(i);
			user = userBaseService.getById(praise.getCreatorId());
			if (i == praiseList.size() - 1) {
				praiseUserList = praiseUserList + user.getNick();
			} else {
				praiseUserList = praiseUserList + user.getNick() + " , ";
			}
		}
		vo.setPraiseUserList(praiseUserList);
	}

	@RequestMapping(value = "/autoStatus")
	@ResponseBody
	public Result autoExpire() {
		Result result = new Result();
		activityBaseService.autoStatus();
		return result;
	}

	/**
	 *
	 * 获取用户参与活动数
	 * 
	 * @param
	 * @return
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/myActivityPage")
	@ResponseBody
	public Result myActivity() {
		Result result = new Result();
		// 查询用户活动id
		Integer userId = super.getUserId();
		ActivitySign activitySign = new ActivitySign();
		activitySign.setUserId(userId);
		List<ActivityBaseVo> page = activityBaseService.queryActivityByUserId(userId);
		String typeName = "";
		for (ActivityBaseVo activityBaseVo : page) {
			if (activityBaseVo.getCostType() == ActivityBaseCostTypeDictEnum.ONLINE.getCode()) {// 在线交易
				activityBaseVo.setShowCost("￥" + activityBaseVo.getCost());
			} else {
				typeName = activitySysDictService.dictValue(ActivityConst.DICTYPE_ACTIVITY_BASE_COSTTYPE,
						String.valueOf(activityBaseVo.getCostType()));
				activityBaseVo.setShowCost(typeName);
			}
			if (activityBaseVo.getType() == ActivityBaseTypeDictEnum.COMMUNITY.getCode()) {// 社区活动
				UserBaseVo userBaseVo = userBaseService.getById(activityBaseVo.getCreatorId());
				activityBaseVo.setSponsorLogo(userBaseVo.getFullLogo());
				activityBaseVo.setSponsorName(userBaseVo.getNick());
				activityBaseVo.setSponsorId(userBaseVo.getId());
			} else {// 圈子活动
				CircleBaseVo circleBaseVo = circleBaseService.getById(activityBaseVo.getBizzId());
				activityBaseVo.setSponsorLogo(circleBaseVo.getFullLogo());
				activityBaseVo.setSponsorName(circleBaseVo.getName());
				activityBaseVo.setSponsorId(circleBaseVo.getId());
			}
			activityBaseVo.setIsShowPraiseList(false);
			addCommentAndPraiseInfo(activityBaseVo);
		}
		result.put("page", page);
		return result;
	}
}
